package com.library.controller.user;

import com.library.context.BaseContext;
import com.library.dto.FavoriteDTO;
import com.library.dto.PageQueryDTO;
import com.library.result.PageResult;
import com.library.result.Result;
import com.library.service.FavoriteService;
import com.library.vo.FavoriteVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user/favorite")
@Api(tags = "用户收藏相关接口")
@Slf4j
public class FavoriteController {

    @Autowired
    private FavoriteService favoriteService;

    @PostMapping
    @ApiOperation("添加收藏")
    public Result<String> save(@RequestBody FavoriteDTO favoriteDTO){
        log.info("添加收藏：{}", favoriteDTO);
        favoriteService.save(favoriteDTO);
        return Result.success();
    }

    @GetMapping("/{userId}")
    @ApiOperation("查询收藏")
    public Result<PageResult> getByUserId(@PathVariable Long userId, PageQueryDTO pageQueryDTO){
        log.info("查询收藏：{}", userId);
        PageResult pageResult = favoriteService.getByUserId(userId, pageQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping("/{bookId}")
    @ApiOperation("删除收藏")
    public Result<String> delete(@PathVariable Long bookId){
        log.info("删除收藏：{}", bookId);
        //获得当前用户的id
        Long userId = BaseContext.getCurrentId();
        favoriteService.delete(bookId, userId);
        return Result.success();
    }

}
